package com.slunk.dodgetrain;

import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

import com.slunk.physics.characters.Powerup;
import com.slunk.physics.characters.SpeedBoostPowerup;
import com.slunk.physics.characters.PointBoostPowerup;
import com.slunk.utilities.EntityCategories;

public class PowerupManager {

	private Array<Powerup>powerups;
	private Array<String> available_powerups;
	private World world;
	private float screenWidth;
	private float screenHeight;

	public PowerupManager (World world, float screenWidth, float screenHeight) {
		this.world = world;
		this.screenHeight = screenHeight;
		this.screenWidth = screenWidth;
		this.powerups = new Array<Powerup>();
		
		this.available_powerups = new Array<String>();
		this.available_powerups.add(SpeedBoostPowerup.class.getSimpleName());
		//this.available_powerups.add(PointBoostPowerup.class.getSimpleName());
	}
	
	public void drawPowerups(SpriteBatch batch) {
	
		for(Powerup powerup : this.powerups) {
			powerup.drawSprite(batch);
		}
	}
	
	public void dispatchNewPowerup() {
		float random_x = MathUtils.random(0, this.screenWidth);
		float random_y = MathUtils.random(0, this.screenHeight);
		
		int randomIndex = MathUtils.random(0, this.available_powerups.size-1);
		String className = this.available_powerups.get(randomIndex);
		System.out.println(randomIndex);
		if (className.compareTo("SpeedBoostPowerup") == 0) {
			System.out.println("spawning speed boost powerup");
			SpeedBoostPowerup powerup = new SpeedBoostPowerup(this.world, new Vector2(random_x,random_y), 7,7,  EntityCategories.POWERUP.getEntityValue(),  (short)(EntityCategories.BOUNDARY.getEntityValue()|EntityCategories.CAR.getEntityValue()) );
			this.powerups.add(powerup);
		} else if (className.compareTo("PointBoostPowerup") == 0) {
			System.out.println("spawning point boost powerup");
			PointBoostPowerup powerup = new PointBoostPowerup(this.world, new Vector2(random_x,random_y), 7,7,  EntityCategories.POWERUP.getEntityValue(),  (short)(EntityCategories.BOUNDARY.getEntityValue()|EntityCategories.CAR.getEntityValue()) );
			this.powerups.add(powerup);
		}
	}
	
	public boolean isBodyPowerup(Body body) {
		for (Powerup powerup : this.powerups) {
			if (powerup.getCharacterBody() == body) {
				return true;
			}
		}
		return false;
	}
	
	public void removeAllPowerups() {
		Array<Powerup> powerups_copy = new Array<Powerup>(this.powerups);
		for (Powerup powerup : powerups_copy) {
			this.removePowerup(powerup);
		}
	}
	
	public void removePowerup(Powerup powerup) {
		if (this.powerups.contains(powerup, true)) {
			this.powerups.removeValue(powerup, true);
		}
	}
}
